/**  
 * @Title: SqlSessionExample.java  
 * @Package com.blog4java.mybatis.sqlsession  
 * @Description: TODO(用一句话描述该文件做什么)  
 * @author suyuhao  
 * @date 2025-05-29 05:16:25   
 */
package com.blog4java.mybatis.sqlsession;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.blog4java.common.DbUtils;
import com.blog4java.mybatis.configuration.entity.UserEntity;
import com.blog4java.mybatis.configuration.mapper.UserMapper;

/**  
* @ClassName: SqlSessionExample  
* @Description: TODO(这里用一句话描述这个类的作用)  
* @author suyuhao  
* @date 2025-05-29 05:16:25
*/
public class SqlSessionExample {
    @Before
    public void initData() {
        DbUtils.initData();
    }
    
    @Test
    public void testSqlSession() throws IOException {
        //获取Mybatis配置文件输入流
        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
        //通过SqlSessionFactoryBuilder创建SqlSessionFactory实例
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //调用SqlSessionFactory的openSession()方法，创建SqlSession实例
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        List<UserEntity> userList = userMapper.listAllUser();
        System.out.println(JSON.toJSONString(userList));
/*        String now = userMapper.selectTime();
        System.out.println(now);*/
    }
}
